﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using MINERP.DAL;
using MINERP.MODEL.Statistics;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;
using MINERP.DAL.Statistics;


namespace MINERP.BLL.Statistics
{
    public class OutRetunrOrderRecordsBLL
    {
        StatisticsDAL dal = new StatisticsDAL();
        public List<OutReturnRecordInfo> GetOutReturnRecords(DateTime startTime,DateTime endTime)
        {
            List<OutReturnRecordInfo> list = new List<OutReturnRecordInfo>();

            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                //生成中
                List<Tb_OrderBag> orderbaglist = enty.Tb_OrderBag.Where(o=>o.OrderTime>=startTime&&o.OrderTime <=endTime).ToList();
                foreach (var orderbag in orderbaglist)
                {
                    if(!orderbag.ShopReference.IsLoaded)
                    {
                        orderbag.ShopReference.Load();
                    }
                    if (!orderbag.orderInfo.IsLoaded)
                    {
                        orderbag.orderInfo.Load();
                    }
                    foreach (var orderInfo in orderbag.orderInfo)
                    {
                        if (orderInfo.OrderType == (int)OrderInfoType.OutReturnInfo)
                        {
                            if(!orderInfo.WorkOrderFlowList.IsLoaded)
                            {
                                orderInfo.WorkOrderFlowList.Load();
                            }
                            string userName = "";
                            //WorkOrderFlowList wfList = orderInfo.WorkOrderFlowList.FirstOrDefault(w => w...tb_Tech.TechName == "开始");
                            WorkOrderFlowList wfList = orderInfo.WorkOrderFlowList.ToList().OrderBy(w => w.WorkOrder).FirstOrDefault();

                            if (wfList != null)
                            {
                                
                                if (!wfList.tb_TechReference.IsLoaded)
                                {
                                    wfList.tb_TechReference.Load();
                                }
                                if (wfList.tb_Tech.TechName=="开始")
                                {
                                    int userId = wfList.UserId;
                                    if (userId != 0)
                                    {
                                        userName = enty.users.First(u => u.userID == userId).UserName;
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                               
                            }
                            OutReturnRecordInfo outreturn = new OutReturnRecordInfo()
                            {
                                CompleteTime = orderInfo.CompleteTime,
                                CtmName = orderInfo.CtmName,
                                Height = orderInfo.Height,
                                OrderPath = orderInfo.OrderPath,
                                OrderProductCode = orderInfo.OrderProductCode,
                                OrderProductName = orderInfo.OrderProductName,
                                PageNum = orderInfo.PageNum,
                                OrderProductId = orderInfo.OrderProductId,
                                ProductNum = orderInfo.productNum,
                                Remark = orderInfo.Remark,
                                ShopName = orderbag.Shop.shopName,
                                Width = orderInfo.Width,
                                UserName = userName
                            };
                            list.Add(outreturn);
                        }
                    }
                    
                }

                //完成
                List<Tb_OrderBag_BK> orderbaglist_bk = enty.Tb_OrderBag_BK.Where(o => o.OrderTime >= startTime && o.OrderTime <= endTime).ToList();
                foreach (var orderbag in orderbaglist_bk)
                {
                    if (!orderbag.ShopReference.IsLoaded)
                    {
                        orderbag.ShopReference.Load();
                    }
                    if (!orderbag.orderInfo_BK.IsLoaded)
                    {
                        orderbag.orderInfo_BK.Load();
                    }
                    foreach (var orderInfo in orderbag.orderInfo_BK)
                    {
                        if (orderInfo.OrderType == (int)OrderInfoType.OutReturnInfo)
                        {
                            if (!orderInfo.WorkOrderFlowList_BK.IsLoaded)
                            {
                                orderInfo.WorkOrderFlowList_BK.Load();
                            }
                            string userName = "";
                            //WorkOrderFlowList_BK wfList = orderInfo.WorkOrderFlowList_BK.FirstOrDefault(w => w.tb_Tech.TechName == "开始");
                            WorkOrderFlowList_BK wfList = orderInfo.WorkOrderFlowList_BK.ToList().OrderBy(w => w.WorkOrder).FirstOrDefault();
                            if (wfList != null)
                            {

                                if (!wfList.tb_TechReference.IsLoaded)
                                {
                                    wfList.tb_TechReference.Load();
                                }
                                if (wfList.tb_Tech.TechName == "开始")
                                {
                                    int userId = wfList.UserId;
                                    if (userId != 0)
                                    {
                                        userName = enty.users.First(u => u.userID == userId).UserName;
                                    }
                                }
                                else
                                {
                                    continue;
                                }

                            }
                            OutReturnRecordInfo outreturn = new OutReturnRecordInfo()
                            {
                                CompleteTime = orderInfo.CompleteTime,
                                CtmName = orderInfo.CtmName,
                                Height = orderInfo.Height,
                                OrderPath = orderInfo.OrderPath,
                                OrderProductCode = orderInfo.OrderProductCode,
                                OrderProductName = orderInfo.OrderProductName,
                                PageNum = orderInfo.PageNum,
                                OrderProductId = orderInfo.OrderProductId,
                                ProductNum = orderInfo.productNum,
                                Remark = orderInfo.Remark,
                                ShopName = orderbag.Shop.shopName,
                                Width = orderInfo.Width,
                                UserName = userName
                            };
                            list.Add(outreturn);
                        }
                    }

                }
             
            }

            return list;
 
        }

        public DataSet GetOutReturnInfo(string salesmanName, DateTime startTime, DateTime endTime)
        {
            return dal.GetSalemanOutReturnOrder(salesmanName, startTime, endTime);
        }


        public List<string> GetSalesmanList()
        {
            List<string> salesmanNameList = new List<string>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<SalesmanBalance> salemanlist = db.SalesmanBalance.ToList();
                foreach (var item in salemanlist)
                {
                    salesmanNameList.Add(item.SalesmanName);
                }
            }
            return salesmanNameList;
        }

    }
}
